package fzu.functiongather.Utils;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;




import java.util.ArrayList;
import java.util.List;

import com.bumptech.glide.Glide;

import fzu.functiongather.CarouselPicture.WebLoadActivity;
import fzu.functiongather.Model.GetBanner;
import fzu.functiongather.R;

/**
 * Created by codingWw on 2016/3/8.
 */
public class BannerViewPager<T extends GetBanner.Img> extends RelativeLayout {

    private Context mContext;
    private View view;
    private ViewPager pagerGuide;
    private LinearLayout ll_slideDot;

    private List<T> banners;
    private List<ImageView> dots;
    private int pagerCount;
    private int currentIndex;

    private Handler mHandler;
    private boolean isRolling;

    public BannerViewPager(Context context) {
        super(context);
        init(context);
    }

    public BannerViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        view = LayoutInflater.from(mContext).inflate(R.layout.i_view_guide_pager, this, true);
    }

    public void createView(List<T> banners)
    {
        if(banners == null || banners.size()<=0) {
            return;
        }
        pagerGuide = (ViewPager)view.findViewById(R.id.vp_pagerGuide);
        ll_slideDot = (LinearLayout)view.findViewById(R.id.ll_slideDot);
        this.banners = banners;
        pagerCount = banners.size();
        dots = new ArrayList<ImageView>();
        isRolling = false;
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                int i = (currentIndex + 1) % pagerCount;
                pagerGuide.setCurrentItem(i);
                if (isRolling)
                    mHandler.sendEmptyMessageDelayed(1, 3000);
            }
        };

        initDot();
        initPager();
    }

    private void initDot() {
        ll_slideDot.removeAllViews();
        for (int i = 0; i < pagerCount; i++) {
            ImageView dot = new ImageView(mContext);
            dot.setBackgroundResource(R.mipmap.gray_circle);
            int side = BitmapTool.dp2px(mContext, 8);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(side, side);
            params.setMargins(5, 5, 5, 5);
            dot.setLayoutParams(params);
            dots.add(dot);
            ll_slideDot.addView(dot);
        }
        dots.get(0).setBackgroundResource(R.mipmap.gree_circle);
    }

    private void initPager() {
        pagerGuide.setAdapter(new GuidePagerAdapter(mContext, banners));
        pagerGuide.setCurrentItem(0);
        pagerGuide.addOnPageChangeListener(new OnGuidePagerChangeListener());
    }

    public void startRoll() {
        if(pagerCount>1 && !isRolling) {
            isRolling = true;
            mHandler.sendEmptyMessageDelayed(1, 3000);
        }
    }

    public void stopRoll() {
        isRolling = false;
    }

    class GuidePagerAdapter extends PagerAdapter {

        private List<T> data;
        private List<ImageView> imgList;

        public GuidePagerAdapter(Context context, List<T> data) {
            super();
            this.data = data;
            imgList = new ArrayList<>();
            for (GetBanner.Img banner : data) {
                ImageView img = new ImageView(context);
                img.setClickable(true);
                img.setScaleType(ImageView.ScaleType.CENTER_CROP);
//                ImageLoadManager.getInstance(mContext).displayImage(banner.imagePath, img);
//                Ion.with(img).load(banner.imagePath);
                Glide.with(mContext)
                        .load(banner.getImgUrl())
                        .centerCrop()
                        .crossFade()
                        .error(R.mipmap.ic_launcher)
                        .into(img);

                img.setOnClickListener(new OnBannerClickListener(banner));
                imgList.add(img);
            }
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(imgList.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(imgList.get(position), 0);
            return  imgList.get(position);
        }
    }

    class OnBannerClickListener implements OnClickListener {

        GetBanner.Img banner;

        public OnBannerClickListener(GetBanner.Img banner) {
            this.banner = banner;
        }
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(mContext, WebLoadActivity.class);
            String url = "";
            if (TextUtils.isEmpty(banner.getLink()) || banner.getLink() == null || banner.getLink() =="") {
//                ToastUtils.showShort(mContext, "url 为空");
            }else {
                url = banner.getLink();
                intent.putExtra("url", url);
                intent.putExtra("banner","banner");
                mContext.startActivity(intent);
            }
        }

    }

    class OnGuidePagerChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int selectedIndex) {
            // TODO Auto-generated method stub
            dots.get(selectedIndex).setBackgroundResource(R.mipmap.gree_circle);
            dots.get(currentIndex).setBackgroundResource(R.mipmap.gray_circle);
            currentIndex = selectedIndex;
        }
    }
}
